<?php

/**
 * 投票模块-候选人管理
 * @desc
 */
class Api_Vote_User extends PhalApi_Api {

    public function getRules() {
        return [
            '*'      => [
                'token'        => ['name' => 'token', 'type' => 'string', 'min' => 12, 'max' => 12, 'require' => true, 'desc' => 'tokenID'],
            ],
            'update' => [
                'id'         => ['name' => 'id', 'type' => 'int', 'require' => true, 'desc' => 'id'],
                'name'       => ['name' => 'name', 'type' => 'string', 'require' => true, 'desc' => '候选人名字'],
                'NO'         => ['name' => 'NO', 'type' => 'string', 'min' => 1, 'max' => 100000, 'require' => false, 'desc' => '候选人编号'],
                'headimgurl' => ['name' => 'headimgurl', 'type' => 'string',  'require' => false, 'desc' => '候选人头像'],
                'poll'       => ['name' => 'poll', 'type' => 'int', 'default' => 0, 'require' => true, 'desc' => '投票数'],


            ],
            'add'    => [
                'itemid'     => ['name' => "itemid", 'type' => 'int', 'require' => true, 'desc' => '所属项目id'],
                'name'       => ['name' => 'name', 'type' => 'string', 'require' => true, 'desc' => '候选人名字'],
                'NO'         => ['name' => 'NO', 'type' => 'string', 'min' => 1, 'max' => 100000, 'require' => false, 'desc' => '候选人编号'],
                'headimgurl' => ['name' => 'headimgurl', 'type' => 'string',  'require' => false, 'desc' => '候选人头像'],
                'poll'       => ['name' => 'poll', 'type' => 'int', 'default' => 0, 'require' => true, 'desc' => '投票数'],


            ],

            'info'  => [
                'id' => ['name' => 'id', 'type' => 'int', 'require' => true, 'desc' => 'id'],
            ],
            'lists'  => [
                'itemid' => ['name' => "itemid", 'type' => 'int', 'require' => true, 'desc' => '所属项目id']
            ],

            'delete' => [
                'id' => ['name' => 'id', 'type' => 'int', 'require' => true, 'desc' => 'id'],
                'itemid' => ['name' => "itemid", 'type' => 'int', 'require' => true, 'desc' => '所属项目id']
            ],


        ];
    }
    /**
     * 候选人资料
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return object data 资料
     * @return string msg 提示信息
     */
    public function info(){
        $db = new Domain_Vote();
        return $db->userinfo($this->id);
    }
    /**
     * 添加候选人
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int code
     * @return object data 添加/更新后的资料
     * @return string msg 提示信息
     */
    public function add() {
        $db = new Domain_Vote();
        $itemInfo = $db->itemGet($this->itemid);
        if (!$itemInfo) {
            throw new PhalApi_Exception("未找到该项目", 10002);
        };
        $data = $db->add([
            "no"         => $this->NO,
            "realname"   => $this->name,
            "item"       => $this->itemid,
            "poll"       => $this->poll,
            "headimgurl" => $this->headimgurl,
        ]);
        if ($data == NULL) {
            throw new PhalApi_Exception("添加失败", 10001);
        }
        return $data;
    }

    /**
     * 更新候选人
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int code
     * @return string msg 提示信息
     */
    public function update() {
        $db = new Domain_Vote();
        if (!$db->hasName($this->id)) {
            throw new PhalApi_Exception("无此用户", 10002);
        }
        $data = $db->userset($this->id, [
            "no"         => $this->NO,
            "realname"   => $this->name,
            "poll"       => $this->poll,
            "headimgurl" => $this->headimgurl,
        ]);
        if ($data == NULL) {
            throw new PhalApi_Exception("更新失败", 10001);
        }
        return [
            'isUpdated' => true
        ];
    }


    /**
     * 删除候选人资料
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return boolean data.isdeleted 是否删除成功
     * @return string msg 提示信息
     */
    public function delete() {
        $db = new Domain_Vote();
        return [
            "isdeleted" => !!$db->delete($this->id, $this->itemid)
        ];
    }


    /**
     * 获取项目投票数列表
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return array data 数据列表
     * @return string name 姓名
     * @return string poll 投票数
     * @return string headimgurl 头像地址
     * @return string no 投票编号 唯一
     * @return string msg 提示信息
     */
    public function lists() {
        $db = new Domain_Vote();
        return $db->getlistbyid($this->itemid);
    }


}
